function setDate(objEnddate, days){
    var dt = new Date(parseInt($dp.cal.getP('y')), parseInt($dp.cal.getP('M'))-1, parseInt($dp.cal.getP('d'))+days);
    objEnddate.value = dt.format("yyyy-MM-dd");
}


$(document).ready(function(){
	try{
		$.jgrid.no_legacy_api = true;
		$.jgrid.useJSON = true;
		$.jgrid.formatter.integer.thousandsSeparator = ',';
		$.jgrid.formatter.number.thousandsSeparator = ',';
		$.jgrid.formatter.currency.thousandsSeparator = ',';
		
		$("#orderItemsTable").jqGrid({						
			url : appRoot + '/bankStatementItem/list/' + bankStatementId + '?ajax=1',
			editurl : appRoot + '/bankStatementItem/save?ajax=1&bankStatementId=' + bankStatementId,
			datatype : 'json',
			mtype : 'GET',
			jsonreader: {             
				repeatitems: false,
				id:'Id',
				root:'rows'
			}, 
			colNames : [ 'id','objectId','purchaseItemId','中文名', '英文名', '产品编号', '数量','单位','单价', '小计'], 
			colModel : [ 
	       {
				name : 'id',
				index : 'id',
				editable : true,
				hidden:true
				
			}, {
				name : 'objectId',
				index : 'objectId',
				editable : true,
				hidden:true
			}, {
				name : 'purchaseItemId',
				index : 'purchaseItemId',
				editable : true,
				hidden:true
			}, {
				name : 'name',
				index : 'name',
				width : 120,
				align : 'left',
				resizable : false,
				editable : true,
				edittype:'text',
				sortable: false,
				editrules : {
					required : true
				}

			}, {
				name : 'englishName',
				index : 'englishName',
				width : 200,
				editable : true,
				edittype:'text',
				sortable: false,
				editrules : {
					required : true
				}
			}, {
	            name : 'productNo',
	            index : 'productNo',
	            width : 110,
	            editable : true,
	            edittype:'text',
	            sortable: false,
	            editrules : {
	                required : true
	            }
	        }, {
	            name : 'quantity',
	            index : 'quantity',
	            width : 60,
	            align : 'right',
	            editable : true,
	            edittype:'text',
	            sortable: false,
	            editrules : {
	                required : true,
	                number : true
	            }                           
	        }, {
				name : 'unit',
				index : 'unit',
				width : 40,
				editable : true,
				edittype:'select',
				formatter:'select',
				editoptions : {
			        value :units
			    },
				editrules : {
					required : true
				}
			}, {
				name : 'unitPrice',
				index : 'unitPrice',
				width : 60,
				editable : true,
				edittype:'text',
				sortable: false,
				editrules : {
					required : true,
					number : true
				}
			}, {
				name : 'amount',
				index : 'amount',
				width : 100,
				align : 'right',
				editable : false,
				sortable: false,
				editrules : {
					number : true
				}
			}
			],
			gridComplete: function(){
				var grid = $("#orderItemsTable");
				var sumQuantity = grid.jqGrid('getCol', 'quantity', false, 'sum'); 
				var sumAmount = grid.jqGrid('getCol', 'amount', false, 'sum'); 
				grid.jqGrid('footerData','set', {productNo:"合计:", quantity: sumQuantity,amount:sumAmount}); 
			},
			width: 730,
			height : 300,
			pager : $('#pager'),
			nowNum : 100,
			rowNum : 100,
			sortname : 'id',
			viewrecords : true,
			footerrow: true,
			shrinkToFit:false,
			caption : '货物资料'
		}).navGrid("#pager",{edit:true,add:false,del:true,search:false});

		
		$("#orderChargeTable1").jqGrid({                       
	        url : appRoot + '/bankStatementCharge/list/1/' + bankStatementId + '?ajax=1',
	        editurl : appRoot + '/bankStatementCharge/save?ajax=1&type=1&bankStatementId=' + bankStatementId,
	        datatype : 'json', 
	        mtype : 'GET',
	        jsonreader: {             
	            repeatitems: false,
	            id:'Id',
	            root:'rows'
	        }, 
	        colNames : [ 'id','objectId','项目', '金额'],
	        colModel : [
	       {
	            name : 'id',
	            index : 'id',
	            editable : true,
	            hidden:true
	            
	        }, {
	            name : 'objectId',
	            index : 'objectId',
	            editable : true,
	            hidden:true
	        },
	        {
	            name : 'reason',
	            index : 'reason',
	            width : 50,
	            align : 'left',
	            resizable : false,
	            editable : true,
	            edittype:'text',
	            sortable: false,
	            editrules : {
	                required : true
	            }

	        }, {
	            name : 'amount',
	            index : 'amount',
	            width : 40,
	            align : 'right',
	            editable : true,
	            sortable: false,
	            editrules : {
	                number : true
	            }
	        }
	        ],
	        gridComplete: function(){
	            var grid = $("#orderChargeTable1");
	            var sumAmount = grid.jqGrid('getCol', 'amount', false, 'sum'); 
	            grid.jqGrid('footerData','set', {reason:"合计:", amount:sumAmount}); 
	        },
	        width: 730,
	        height : 100,
	        pager : $('#pager1'),
	        nowNum : 100,
	        rowNum : 100,
	        sortname : 'id',
	        viewrecords : true,
	        footerrow: true,
	        caption : '其它费用'
	    }).navGrid("#pager1",{edit:true,add:true,del:true,search:false});
		
		$("#orderChargeTable2").jqGrid({                       
	        url : appRoot + '/bankStatementCharge/list/2/' + bankStatementId + '?ajax=1',
	        editurl : appRoot + '/bankStatementCharge/save?ajax=1&type=2&bankStatementId=' + bankStatementId,
	        datatype : 'json', 
	        mtype : 'GET',
	        jsonreader: {             
	            repeatitems: false,
	            id:'Id',
	            root:'rows'
	        }, 
	        colNames : [ 'id','objectId','项目', '金额'],
	        colModel : [
	       {
	            name : 'id',
	            index : 'id',
	            editable : true,
	            hidden:true
	            
	        }, {
	            name : 'objectId',
	            index : 'objectId',
	            editable : true,
	            hidden:true
	        },
	        {
	            name : 'reason',
	            index : 'reason',
	            width : 50,
	            align : 'left',
	            resizable : false,
	            editable : true,
	            edittype:'text',
	            sortable: false,
	            editrules : {
	                required : true
	            }

	        }, {
	            name : 'amount',
	            index : 'amount',
	            width : 40,
	            align : 'right',
	            editable : true,
	            sortable: false,
	            editrules : {
	                number : true
	            }
	        }
	        ],
	        gridComplete: function(){
	            var grid = $("#orderChargeTable2");
	            var sumAmount = grid.jqGrid('getCol', 'amount', false, 'sum'); 
	            grid.jqGrid('footerData','set', {reason:"合计:", amount:sumAmount}); 
	        },
	        width: 730,
	        height : 100,
	        pager : $('#pager2'),
	        nowNum : 100,
	        rowNum : 100,
	        sortname : 'id',
	        viewrecords : true,
	        footerrow: true,
	        caption : '扣款'
	    }).navGrid("#pager2",{edit:true,add:true,del:true,search:false});
		
	}catch(e){}
	
	$(".colorbox").colorbox({iframe:true, innerWidth:700, innerHeight:460});
    
	$("#purchaseId").change(function(){
        var purchaseIds = $(this).val();
        if(purchaseIds){
            if(confirm("需要导入该采购单的明细数据吗？")){
                $("#notice").html("数据导入中...").show();
                 $.ajax({
                     url: appRoot + "/purchaseItem/importToBankStatement/" + bankStatementId + "?purchaseIds=" + purchaseIds  + "&date=" + (new Date()).valueOf(),
                     type:'post',
                     dataType:'json',
                     success:function(msg){
                         if(msg.result){
                             $("#orderItemsTable").trigger("reloadGrid");
                             $("#notice").html("成功导入" + msg.result + "条采购明细");
                         }else{
                        	 $("#notice").html("没有明细需要导入");
                         }
                     }
                });
            }
        }
    });
	 
});

//page

function getSupplier(id){
    $("#suberror").hide();
    $("#subnotice").hide();
    $("#otherUsers").val("");
    $("input[type='checkbox']").each(function(){
        $(this).attr("checked",false);
    });
    if(id){
        $.ajax({
             url: appRoot + "/supplier/get/" + id,
             type:'post',
             dataType:'json',
             success:function(msg){
                 $("#customer_2").text(msg.entity.name).attr("title",msg.entity.email);
             }
        });
    }
}

function confirmOk(orderId,action){
    $(".audit").attr("disabled",true);
    if(confirm("你确认正确？")){
    	$.ajax({
            url: appRoot + "/bankStatement/confirm/" + orderId,
            type:'post',
            dataType:'json',
            data:"status=2" ,
            success:function(msg){
                if(msg.result){
                    window.location.href= appRoot + '/bankStatement/' + action +'?message=' + encodeURIComponent('操作成功');
                }else{
                    showMessage("操作失败");
                    $(".audit").attr("disabled",false);
                }
            }
       });
    }
}

function audit(orderId,action){
    $(".audit").attr("disabled",true);
    if(confirm("你确认审核通过？")){
    	$.ajax({
            url: appRoot + "/bankStatement/audit/" + orderId,
            type:'post',
            dataType:'json',
            data:"status=2" ,
            success:function(msg){
                if(msg.result){
                    window.location.href= appRoot + '/bankStatement/' + action +'?message=' + encodeURIComponent('操作成功');
                }else{
                    showMessage("操作失败");
                    $(".audit").attr("disabled",false);
                }
            }
       });
    }
}

function notify(){
    if(currentOrderId==null){
        return;
    }
    $(".audit").attr("disabled",true);
    $("#notifyButton").attr("disabled",true);
    var selected = getCheckedValuesString(document.getElementsByName("users"));
    var customer = ($("#customer_1").attr("checked")=="checked");
    var others = $("#otherUsers").val();
    if(!selected&&!customer&&!others){
        $("#suberror").html("请至少选择一个接收者").show();
        $(".audit").attr("disabled",false);
        $("#notifyButton").attr("disabled",false);
    }else{
        $("#suberror").hide();
        $("#subnotice").html("操作进行中...").show();
        $.ajax({
             url:appRoot + "/bankStatement/notify/" + currentOrderId + "?date=" + (new Date()).valueOf(),
             type:'post',
             dataType:'json',
             data:'purchasers=' + (selected==null?"":selected) + "&customer=" + customer + "&others=" + others,
             success:function(msg){
                 if(msg.result){
                     showMessage("邮件已发送");
                 }else{
                     showMessage("操作失败");
                 }
                 $(".audit").attr("disabled",false);
                 $("#notifyButton").attr("disabled",false);
                 jQuery.fn.colorbox.close();
             }
        });
    }
}

function pay(orderId,action){
    $(".audit").attr("disabled",true);
    if(confirm("你确认已付款？")){
    	$.ajax({
            url: appRoot + "/bankStatement/pay/" + orderId,
            type:'post',
            dataType:'json',
            success:function(msg){
                if(msg.result){
                    window.location.href= appRoot + '/bankStatement/' + action + '?message=' + encodeURIComponent('操作成功');
                }else{
                    showMessage("操作失败");
                    $(".audit").attr("disabled",false);
                }
            }
       });
    }
}